// /**
//  * 通用的ajax函数
//  * created by 唐智杰 2021-02-22
//  */

// 引入axios 和 qs
import axios from 'axios'
import Qs from 'qs'
import {Message} from 'element-ui'; // 引入Message弹框组件

// // 引入本地存储
// import local from '@/utils/local'

// 默认请求api
// axios.defaults.baseURL = 'http://192.168.31.226:8088/admin'
// axios.defaults.baseURL = 'http://49.235.146.181:8088/admin'
// axios.defaults.baseURL = 'http://localhost:8088/admin/'
// axios.defaults.baseURL = '/zhbg/'
// axios.defaults.baseURL = window.serverUrl.url;

// 请求拦截器
axios.interceptors.request.use(config => {
    // const token = local.get('t_k') || ''

    // if (token) {
    //   config.headers.Authorization = token
    // }
    config.headers['Content-Type'] = 'application/json'
    return config
})

const REQUESTCODE = {
    SUCCESS: 200,
    PARAMETER_ERROR: 203,
    BUSINESS_ID_NOT_EXIST: 206,
    SYSTEM_ERROR: 300,
    TOKEN_ERROR: 301,
    TOKEN_IS_BLANK: 302,
    DELETE_ERROR: 204,
    UPDATE_ERROR: 205,
    LOGIC_ERROR: 701
}

// 响应拦截器
axios.interceptors.response.use(response => {
    // 拦截响应数据 统一处理
    if (response.hasOwnProperty('status') && response.hasOwnProperty('data')) {
        let code = response.data.code
        let msg = response.data.msg
        if (code !== REQUESTCODE.SUCCESS) { // 如果等于1 统一弹出失败提示框
            if (code === REQUESTCODE.LOGIC_ERROR) {
                this.$message.error('逻辑条件错误')
            } else {
                if(msg.indexOf('Violate not null constraint on')<0){
                    Message.error(msg)
                }
            }
        }
    }
    // console.log(response);
    return response;
})

// get请求 和 post请求
const req = {
    get(url, params = {}, type) {
        return new Promise((resolve, reject) => {
            if (type === "other") {
                url = window.serverUrl.routerPath +url
            } else if (type === "download") {
                url = window.serverUrl.routerPath +url
            } else {
                url = window.serverUrl.url +url
            }
            axios.get(url, {
                params
            }).then(response => {
                resolve(response.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    post(url, params = {}, type) {
        return new Promise((resolve, reject) => {
            if (type === "other") {
                url = window.serverUrl.routerPath +url
            } else if (type === "download") {
                url = window.serverUrl.routerPath +url
            } else {
                url = window.serverUrl.url +url
            }
            axios.post(url, params.data).then(response => {
                resolve(response.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    put(url, params = {}, type) {
        return new Promise((resolve, reject) => {
            if (type === "other") {
                url = window.serverUrl.routerPath +url
            } else if (type === "download") {
                url = window.serverUrl.routerPath +url
            } else {
                url = window.serverUrl.url +url
            }
            axios.put(url, params.data).then(response => {
                resolve(response.data)
            }).catch(err => {
                reject(err)
            })
        })
    },
    delete(url, data = {}, type) {
        return new Promise((resolve, reject) => {
            if (type === "other") {
                url = window.serverUrl.routerPath + url
            } else if (type === "download") {
                url = window.serverUrl.routerPath +url
            } else {
                url = window.serverUrl.url +url
            }
            axios.delete(url, {data: data.params}).then(response => {
                resolve(response.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

// 暴露请求对象
export default req
